﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using AutoLoL.Logic;
using AutoLoL.Logic.Parsers;
using AutoLoL.Entities;

namespace AutoLoL.Playground
{
    /// <summary>
    /// Interaction logic for RafBrowser.xaml
    /// </summary>
    public partial class RafBrowser : Window
    {
        public RafBrowser()
        {
            InitializeComponent();

            RafManager manager = RafManager.Instance;

            List<Item> items = new List<Item>();

            foreach (RafArchive archive in manager.Archives)
            {
                Item archiveItem = new Item(archive.Version.ToString());

                foreach (RafFile file in archive.Files)
                {
                    Item parentItem = archiveItem;

                    string[] chunks = file.Path.Split(new char[] { '/' });
                    foreach (string chunk in chunks)
                    {
                        Item item = parentItem.GetChildByName(chunk);

                        if (item == null)
                        {
                            item = new Item(chunk);
                            parentItem.Children.Add(item);
                        }

                        parentItem = item;
                    }
                }

                items.Add(archiveItem);
            }

            treeView1.ItemsSource = items;
        }
    }

    internal class Item
    {
        public Item(string name)
        {
            Name = name;
            Children = new List<Item>();
        }

        public string Name { get; set; }

        public List<Item> Children { get; set; }

        public Item GetChildByName(string name)
        {
            return Children.Where(c => c.Name.ToLower().Equals(name.ToLower())).SingleOrDefault();
        }
    }
}
